# Seymore - Content Management on Rails
# 
# Copyright (c) 2006 - 2007 Thomas Mango
# 
# For license information, please visit:
# http://code.google.com/p/seymore/
# 
# File created by Thomas Mango [tsmango@gmail.com]

class PageController < ApplicationController
  def index
    if User.count == 0
      redirect_to :controller => "users", :action => "index"
    else
      @page = Page.find_by_slug("home")
      if session[:user] == nil
        @revision = Revision.find_most_recent_published(@page)
        if @revision.redirect_to != nil
          @page = Page.find(@revision.redirect_to)
          @revision = Revision.find_most_recent_published(@page)
        end
      else
        @revision = Revision.find_most_recent(@page)
      end
      @show_nav = false
      @revision
      render :action => "show"
    end
  end

  def show
    @page = Page.find_by_slug(params[:id])
    
    # this will be removed for 1.0
    if @page == nil
      @page = Page.find_by_name(params[:id])
    end
    
    if session[:user] == nil
      @revision = Revision.find_most_recent_published(@page)
      if @revision.redirect_to != nil
        @page = Page.find(@revision.redirect_to)
        @revision = Revision.find_most_recent_published(@page)
      end
    else
      @revision = Revision.find_most_recent(@page)
    end
    
    @show_nav = (@page.slug == "home" or (session[:user] == nil and @page.parent == nil and @page.children.size == 0)) ? false : true
    @revision
  rescue
    flash[:notice] = "Sorry, the page '#{params[:id]}' doesn't exist!"
    redirect_to :action => "error"
  end

  def add
    return no_access unless user_can?('modify')
    @page = Page.new
    @page.parent_id = params[:parent]
    @revision = Revision.new
    @revision.page = @page
  end
  
  def create
    return no_access unless user_can?('modify')
    @page = Page.new(params[:page])
    @revision = Revision.new(params[:revision])
    @revision.user = session[:user]
    if @page.save
      @revision.page = @page
      if @revision.save
        flash[:notice] = 'Page was successfully created.'
        redirect_to :action => 'show', :id => @page.slug
      else
        redner :action => 'add'
      end
    else
      @revision.page = @page
      render :action => 'add'
    end
  end

  def edit
    return no_access unless user_can?('modify')
    @page = Page.find_by_slug(params[:id])
    @revision = Revision.find_most_recent(@page)
  end

  def save
    return no_access unless user_can?('modify')
    @page = Page.find_by_slug(params[:id])
    @revision = Revision.new(params[:revision])
    @revision.user = session[:user]
    if @page.update_attributes(params[:page])
      @revision.page = @page
      if @revision.save
        flash[:notice] = 'Page was successfully saved.'
        redirect_to :action => 'show', :id => @page.slug
      else
        render :action => 'add'
      end
    else
      render :action => 'add'
    end
  end
  
  def publish
    return no_access unless user_can?('publish')
    @page = Page.find_by_slug(params[:id])
    @revision = Revision.find_most_recent(@page)
    @revision.published = true
    if @revision.save
      flash[:notice] = 'Revision was successfully published.'
      redirect_to :action => 'show', :id => @page.slug
    else
      redner :action => 'show', :id => @page.slug
    end
  end
  
  def revisions
    return no_access unless user_can?('modify')
    @page = Page.find_by_slug(params[:id])
    @revision = Revision.find_most_recent(@page)
  end
  
  def revision
    return no_access unless user_can?('modify')
    @revision = Revision.find(params[:id])
  end
  
  def rollback
    return no_access unless user_can?('modify')
    @old_revision = Revision.find(params[:id])
    @revision = Revision.new
    @revision.page = @old_revision.page
    @revision.content = @old_revision.content
    @revision.user = @old_revision.user
    if @revision.save
      flash[:notice] = 'Page was successfully rolled back'
      redirect_to :action => 'show', :id => @revision.page.slug
    else
      redirect_to :action => 'revisions'
    end
  end
  
  def un_publish
    return no_access unless user_can?('publish')
    @revision = Revision.find(params[:id])
    if @revision.page.slug != 'home' or (@revision.page.slug == 'home' and !@revision.only?)
      @revision.published = false
      if @revision.save
        flash[:notice] = 'Revision was successfully un-published'
        redirect_to :action => 'revisions', :id => @revision.page.slug
      else
        redirect_to :action => 'revisions', :id => @revision.page.slug
      end
    else
      flash[:notice] = "There must be at least one published revision of the 'Home' page."
      redirect_to :action => "index"
    end
  end
  
  def reorder
    return no_access unless user_can?('publish')
    if params[:id] == nil
      @parent = Page.find_by_slug('home')
      @pages = Page.find(:all, :order => "position", :conditions => "parent_id is null")
    else  
      @parent = Page.find(params[:id])
      @pages = Page.find(:all, :order => "position", :conditions => ["parent_id = ?", @parent.id])
    end
  end
  
  def update_page_positions
    return no_access unless user_can?('publish')
    params[params[:id]].each_with_index do |id, position|
      Page.update(id, :position => position+1) if id != ''
    end
    render :nothing => true
  end
  
  def remove
    return no_access unless user_can?('remove')
    @page = Page.find_by_slug(params[:id])
    @revision = Revision.find_most_recent(@page)
    if @revision.destroy
      if @page.revisions.size == 0
        @page.destroy
        flash[:notice] = 'Page was successfully removed'
        redirect_to :action => "index"
      else
        flash[:notice] = 'Revision was successfully removed'
        redirect_to :action => 'show', :id => @page.slug
      end
    else
      redirect_to :action => 'show', :id => @page.slug
    end
  end
end
